package cn.izeno.wx.service

import cn.izeno.wx.annotations.XmlRes
import cn.izeno.wx.models.mp.InitRes
import cn.izeno.wx.models.param.WebWxInitParam
import cn.izeno.wx.models.web.*
import io.reactivex.rxjava3.core.Observable
import okhttp3.ResponseBody
import retrofit2.http.*
import java.io.File


/**
 * 个人微信号 Web Api
 */
interface WebWxService {

  /**
   * 获取 uuid ，用于获取登录二维码
   */
  @GET("https://login.weixin.qq.com/jslogin")
  fun uuid(
      @Query("appid") appId: String = "wx782c26e4c19acffb",
      @Query("fun") _fun: String = "new",
      @Query("lang") lang: String = "zh_CN",
      @Query("_") timestamp: Long = System.currentTimeMillis()
  ): Observable<String>

  @GET("https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login")
  fun login(
      @Query("uuid") uuid: String,
      @Query("tip") tip: Int = 0,
      @Query("loginicon") loginIcon: Boolean = false,
      @Query("_") timestamp: Long = System.currentTimeMillis(),
      @Query("r") r: Long = timestamp / 1579

  ): Observable<String>


  /**
   * 不扫码登陆, 不做
   */
  @POST("webwxpushloginurl")
  fun pushLogin()


  /**
   * 修改好友备注
   */
  @POST("webwxoplog?lang=zh_CN&pass_ticket=%s")
  fun remarkName()


  /**
   * 退出微信
   */
  @POST("/webwxlogout")
  fun logout()

  @GET("https://login.weixin.qq.com/qrcode/{uuid}")
  fun getQrcode(@Path("uuid") uuid: String): Observable<String>

  /** 重定向登录 */
  @GET
  @XmlRes
  fun redirect(@Url redirectUrl: String): Observable<WebWxRedirectRes>

  /**
   * 初始化
   */
  @POST("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxinit")
  fun init(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("pass_ticket") ticket: String = WebWxApp.passTicket,
      @Body param: WebWxInitParam = WebWxInitParam(),
      @Query("r") r: Long = System.currentTimeMillis() / 3158
  ): Observable<InitRes>


  /**
   * 微信状态通知
   */
  @POST("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify")
  fun statusNotify(
      @Path("sld") sld: String = WebWxApp.sld,
      @Body body: NotifyParam = NotifyParam()
  ): Observable<String>

  /**
   * 被动添加好友
   */
  @POST("/webwxverifyuser?r=%s&lang=zh_CN&pass_ticket=%s")
  fun verifyUser()

  /**
   * 获取微信联系人
   */
  @GET("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact")
  fun getContact(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("skey", encoded = true) skey: String = WebWxApp.skey,
      @Query("seq") seq: Int = 0,
      @Query("r") r: Long = System.currentTimeMillis()
  ): Observable<ContactRes>

  /**
   * 获取群成员
   */
  @POST("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact")
  fun batchContact(
      @Path("sld") sld: String = WebWxApp.sld,
      @Body body: BatchContactParam,
      @Query("type") type: String = "ex",
      @Query("r") r: Long = System.currentTimeMillis()
  ): Observable<ContactRes>


  /**
   * web微信消息同步
   */
  @POST("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxsync")
  fun sync(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("skey", encoded = true) skey: String = WebWxApp.skey,
      @Query("lang") lang: String = "zh_CN",
      @Body body: WebWxSyncParam = WebWxSyncParam()
  ): Observable<SyncRes>

  /**
   * 检测心跳
   */
  @GET("https://webpush.{sld}.qq.com/cgi-bin/mmwebwx-bin/synccheck")
  fun syncCheck(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("sid") sid: String = WebWxApp.sid,
      @Query("uin") uin: String = WebWxApp.uin,
      @Query("deviceid") deviceId: String = WebWxApp.deviceId,
      @Query("synckey") syncKey: String = WebWxApp.syncKey.string,
      @Query("r") r: Long = System.currentTimeMillis(),
      @Query("_") d: Long = System.currentTimeMillis()
  ): Observable<CheckSyncRes>


  /**
   * 发送消息
   * URL	webwxsendmsg
   */
  @POST("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg")
  @Headers("Cookie:mm_lang=zh_CN; webwxuvid=0132263805badd70736981669fe5ca1d55fb96a6222fd4cf0ec3f16aeb95e61cef3fc37190c655526865c0f16d8e89ec; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; pgv_pvi=1380016128; pgv_si=s3530201088; refreshTimes=4; wxuin=559743960; wxsid=JlzPn1yYR9JAoXne; webwx_data_ticket=gSf0h9tcTsnf8SUej5udZ42X; webwx_auth_ticket=CIsBEMmL2dELGoABnjlt4OObPS08cmmphbPpCDir0IzCgSoUdeelp+M+ARl8c6oy26TTohLLXtaWjY1cvniXUi4jtgHsNU+v/vbd4PFOiWL34ldVrViTHfPogyDiVyCiG/bR1fGBC2CmO19RI47J+CkGDgki83fut3K0YrcSgYklTPdpZ9xKA/2mhLg=; wxloadtime=1545466813_expired; wxpluginkey=1545467316")
  fun sendMsg(
      @Path("sld") sld: String = WebWxApp.sld,
      @Body body: Any
  ): Observable<String>


  /**
   * 上传文件到服务器
   */
  @POST("/webwxuploadmedia?f=json")
  fun uploadMedia()


  /**
   * 下载文件
   */
  @GET("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia")
  @Headers("Range:bytes=0-")
  @Streaming
  fun getMedia(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("skey", encoded = true) skey: String = WebWxApp.skey,
      @Query("sender") fromUsername: String,
      @Query("mediaid") mediaId: String,
      @Query("filename") filename: String
  ): Observable<ResponseBody>


  /**
   * 下载图片消息
   */
  @GET("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg")
  @Streaming
  fun getMsgImg(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("msgid") msgId: String,
      @Query("skey", encoded = true) skey: String = WebWxApp.skey
  ): Observable<ResponseBody>


  /**
   * 下载语音消息
   */
  @GET("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice")
  @Streaming
  fun getVoice(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("msgid") msgId: String,
      @Query("skey", encoded = true) skey: String = WebWxApp.skey
  ): Observable<ResponseBody>

  /**
   * 下载视频消息
   */
  @GET("https://{sld}.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo")
  @Streaming
  @Headers("Range:bytes=0-")
  fun getVideo(
      @Path("sld") sld: String = WebWxApp.sld,
      @Query("msgid") msgId: String,
      @Query("skey", encoded = true) skey: String = WebWxApp.skey
  ): Observable<ResponseBody>
}
